package com.jihox.abookpro.adapter;

import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.jihox.abookpro.R;
import com.jihox.abookpro.ShowImageActivity;
import com.jihox.abookpro.util.AndroidImageFile;
import com.jihox.abookpro.util.DensityUtil;
import com.jihox.abookpro.util.NativeImageLoader;
import com.jihox.abookpro.util.NativeImageLoader.NativeImageCallBack;
import com.jihox.abookpro.view.JihoxImageView;
/**
 * 
 */
public class DragAdapter extends BaseAdapter{
	private List<AndroidImageFile> selectList;
	private LayoutInflater mInflater;
	private int mHidePosition = -1;
	Resources resources;  
	private Point mPoint;
	private GridView mGridView;
	private Context context;
	public DragAdapter(Context context, List<AndroidImageFile> list, GridView mGridView){
		this.selectList = list;
		this.mGridView = mGridView;
		this.context = context;
		this.resources = context.getResources();
		mPoint = new Point(DensityUtil.dip2px(context,resources.getInteger(R.integer.selected_photo_width))
				,DensityUtil.dip2px(context,resources.getInteger(R.integer.selected_photo_height)));
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return selectList.size();
	}

	@Override
	public Object getItem(int position) {
		return selectList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		JihoxImageView mImageView;
		AndroidImageFile aif = selectList.get(position);
		TextView mTextView;
		//		if(convertView == null){
		convertView = mInflater.inflate(R.layout.drag_item, null);
		mImageView = (JihoxImageView) convertView.findViewById(R.id.item_image);
		mTextView = (TextView) convertView.findViewById(R.id.selected_child_number);
		if(position==0){
			mTextView.setText(resources.getString(R.string.T_Cover_Page_Text));
		}else{
			mTextView.setText(" "+position+" ");
		}
		convertView.setTag(mImageView);
		//		}else{
		//			mImageView = (ImageView) convertView.getTag();
		//			mImageView.setImageResource(R.drawable.friends_sends_pictures_no);
		//		}
		mImageView.setTag(aif);
		//利用NativeImageLoader类加载本地图片
		Bitmap bitmap = NativeImageLoader.getInstance().loadNativeImage(aif, mPoint,context, new NativeImageCallBack() {

			@Override
			public void onImageLoader(Bitmap bitmap, AndroidImageFile aif) {
				JihoxImageView mImageView = (JihoxImageView) mGridView.findViewWithTag(aif);
				if(mImageView!=null){
					if(bitmap != null && !bitmap.isRecycled() && mImageView != null){
						mImageView.setTag(aif);
						try{
							mImageView.setImageBitmap(bitmap);
						}catch(Exception e){
							
						}
						
					}
					mImageView.setAdjustViewBounds(true);
				}
			}
		});

		if(bitmap != null && !bitmap.isRecycled()){
			mImageView.setImageBitmap(bitmap);
		}else{
			mImageView.setImageResource(R.drawable.pictures_no);
		}
		mImageView.setAdjustViewBounds(true);

		LayoutParams params = new LayoutParams(ShowImageActivity.selected_photo_width, ShowImageActivity.selected_photo_height);
		convertView.setLayoutParams(params);
		if(position == mHidePosition){
			convertView.setVisibility(View.INVISIBLE);
		}
		//		convertView.setPadding(5, 50, 5, 50);
		return convertView;
	}

	public void setHideItem(int hidePosition){
		this.mHidePosition = hidePosition; 
		this.notifyDataSetChanged();
	}

	public void notifyDataSetChangedForce() {
		NativeImageLoader.getInstance().clearCache();
		super.notifyDataSetChanged();
	}
}
